# 配置文件
import os
import redis
from celery.schedules import crontab


basedir = os.path.abspath(os.path.dirname(__file__))

# 基类
class Config:
    DOMAIN='域名'
    SECRET_KEY = '密钥'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://用户名:密码@域名:端口/blog?charset=utf8mb4'
    SQLALCHEMY_BINDS={
        'blog':'mysql+pymysql://用户名:密码@域名:端口/blog?charset=utf8mb4',
        'csdn':'mysql+pymysql://用户名:密码@域名:端口/csdn?charset=utf8',
        'yuwuzheng': 'mysql+pymysql://用户名:密码@域名:端口/yuwuzheng?charset=utf8'
    }
    # redis
    REDIS_URI='redis:/:密码@域名:端口/4'
    # mysql
    MYSQL_NAME = '数据库名'
    MYSQL_USER = '数据库用户名'
    MYSQL_HOST = "数据库域名"
    MYSQL_PW = "数据库密码"
    MYSQL_CHARSET='utf8mb4' # 编码
    MYSQL_PORT = 3306  # 数据库端口,默认3306
    # cache
    CACHE_TYPE = 'redis'
    CACHE_REDIS_PORT = 6379  # redis,默认6379
    CACHE_REDIS_DB = '库'
    CACHE_REDIS_PASSWORD = '密码'
    CACHE_REDIS_HOST = 'redis.fzj.com'

    # 邮箱
    MAIL_SERVER = 'smtp.qq.com'
    MAIL_PORT = 465
    MAIL_USE_TLS = False
    MAIL_USE_SSL = True
    MAIL_USERNAME = "邮箱"
    MAIL_PASSWORD = "邮箱密码"
    MAIL_DEFAULT_SENDER = "邮箱"
    # celery
    CELERY_BROKER_URL= 'redis://:密码@域名:端口/7'
    CELERY_RESULT_BACKEND= 'redis://:密码@域名:端口/7'
    CELERY_TASK_SERIALIZER='json'
    CELERY_ACCEPT_CONTENT= ['pickle', 'json', 'yaml']
    CELERY_ENABLE_UTC = True  # 启用UTC时区
    CELERY_TIMEZONE = 'Asia/Shanghai'  # 上海时区
    CELERY_HIJACK_ROOT_LOGGER = False  # 拦截根日志配置
    CELERYD_MAX_TASKS_PER_CHILD = 2  # 每个进程最多执行2个任务后释放进程（再有任务，新建进程执行，解决内存泄漏）
    CELERY_IMPORTS=['app.tasks.task']
    CELERYBEAT_SCHEDULE = {
        'update_sitemap_xml': {
            "task": "app.tasks.task.update_sitemap_xml",
            "schedule": crontab(hour=3, minute=30, day_of_week=1),  # 每7天执行一下update_sitemap函数
            "args": (),  # 参数
        },
        'update_sitemap_by_news': {
            "task": "app.tasks.task.update_sitemap_by_news",
            "schedule": crontab(hour=7, minute=30),  # 每天执行一下update_sitemap函数
            "args": (),  # 参数
        },
        'update_promotion': {
            "task": "app.tasks.task.update_promotion",
            "schedule": crontab(hour=7, minute=30, day_of_month='*/3'),  # update_promotion
            "args": (),  # 参数
        },
    }
    # session
    PERMANENT_SESSION_LIFETIME=3600*24*7
    SESSION_TYPE = 'redis'  # session类型为redis
    SESSION_KEY_PREFIX='zimu_'
    SESSION_PERMANENT= True  # 如果设置为True，则关闭浏览器session就失效。
    SESSION_USE_SIGNER= True  # 是否对发送到浏览器上session的cookie值进行加密
    SESSION_REDIS = redis.StrictRedis(host=CACHE_REDIS_HOST, port=CACHE_REDIS_PORT,
                                      password=CACHE_REDIS_PASSWORD, db=4)
    # 端口
    PORT_LIST=[5060,5061,5062]

    # #sitemap
    # SITEMAP_GZIP = True
    # SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS = True
    # SITEMAP_MAX_URL_COUNT = 10
    # from app.views import common
    # SITEMAP_VIEW_DECORATORS = [common.load_page]
    # OSS
    AccessKey = 'AccessKey'
    AccessSession = "AccessSession"
    OssURL = "oss链接地址"
    BucketName = 'bucjet块名字'

    @staticmethod
    def init_app(app):
        pass

# 开发环境
class DevelopmentConfig(Config):
    DEBUG = True
    MAIL_DEBUG = DEBUG

# 生产环境
class ProductionConfig(Config):
    DEBUG = False
    MAIL_DEBUG = DEBUG


# 设置一个config字典，注册不同的配置环境
config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}